﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MDichVu
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();

        public List<DichVu> FindAll()
        {
            var v = from f in context.DichVus orderby f.MaDV descending select f;
            return v.ToList();
        }
        public int GetTotal_DichVu()
        {
            return FindAll_DichVu().Count();
        }
        public int GetTotal_DichVuMoi()
        {
            return FindAll_Web().Count();
        }
        public string GetTitleByMa(int ma)
        {
            var v = from f in context.DichVus.Where(d => d.MaDV == ma) select f.TenDV;
            return v.FirstOrDefault();
        }

        public string GetNoiDung(int ma)
        {
            var v = from f in context.DichVus.Where(d => d.MaDV == ma) select f.NoiDung;
            return v.FirstOrDefault();
        }
        public List<DichVu> FindAll_DichVu()
        {
            var v = from f in context.DichVus.Where(d=>d.HienThi==true) orderby f.MaDV descending select f;
            return v.ToList();
        }
        public List<DichVu> GetList_DichVu(int start, int offset)
        {
            var v = new List<DichVu>();
            v = FindAll_DichVu().Skip(start).Take(offset).ToList();
            return v;
        }
        public List<DichVu> FindAll_Web()
        {
            var v = from f in context.DichVus.Where(d => d.HienThi == true) orderby f.MaDV descending select f;
            return v.ToList();
        }
        public List<DichVu> GetList(int start, int offset)
        {
            var v = new List<DichVu>();
            v = FindAll().Skip(start).Take(offset).ToList();
            return v;
        }
        public DichVu ChiTiet(int id)
        {
            var v = from f in context.DichVus.Where(d => d.MaDV == id) select f;
            return v.SingleOrDefault();
        }
      
        public int GetTotalDichVu()
        {
            return FindAll().Count();
        }
        public void Save(DichVu tt)
        {
            context.DichVus.InsertOnSubmit(tt);
            context.SubmitChanges();
        }
        public void Update(DichVu tt)
        {
            var v = from f in context.DichVus
                    select f;
            DichVu ttdb = v.SingleOrDefault(d => d.MaDV.Equals(tt.MaDV));
            ttdb.TenDV = tt.TenDV;
            ttdb.NoiDung = tt.NoiDung;
          
            ttdb.ThuTu = tt.ThuTu;
            context.SubmitChanges();
        }
       
        
        public DichVu GetByID(int id)
        {
            return (FindAll().Where(d => d.MaDV == id)).SingleOrDefault();
        }


        public int GetMax()
        {
            return (from t in context.DichVus
                    orderby t.MaDV descending
                    select t.MaDV).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.DichVus
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
        public string Delete(int ma)
        {
            try
            {

                var v = from p in context.DichVus
                        where p.MaDV.Equals(ma)
                        select p;
                DichVu spdb = v.SingleOrDefault();
                context.DichVus.DeleteOnSubmit(spdb);
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public string HienThi(int id)
        {
            try
            {
                var v = from f in context.DichVus
                        select f;
                DichVu lspdb = v.SingleOrDefault(d => d.MaDV.Equals(id));
                if (lspdb.HienThi == true)
                {
                    lspdb.HienThi = false;
                }
                else lspdb.HienThi = true;
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}